"""
Standard Pixmap Table v0.2.1

author: MagicSteve
"""
import pyperclip
from PySide6.QtCore import QSize, Qt
from PySide6.QtGui import QFont, QGuiApplication
from PySide6.QtWidgets import QWidget, QVBoxLayout, QStyle, QPushButton, QGridLayout, QLabel, QCheckBox, \
    QHBoxLayout, QGroupBox, QLineEdit, QApplication


class Main_Window_Test(QWidget):
    # 控制参数
    FixedWindowSize = False

    # 成员变量
    MaxColumn = 4
    TitlePointSize = 24
    SubTitlePointSize = 12
    IconSize = 20
    preview_window = None

    def __init__(self):
        super().__init__()
        self.setWindowIcon(self.style().standardPixmap(QStyle.StandardPixmap.SP_TitleBarMenuButton))
        self.setWindowTitle("PySide6 内置标准图形一览表 by MagicSteve")

        # 标题与副标题
        temp_font = QFont("微软雅黑", self.TitlePointSize)
        temp_font.setBold(True)
        self.label_title = QLabel("内置标准图形一览表")
        self.label_title.setFont(temp_font)
        self.label_title.setAlignment(Qt.AlignmentFlag.AlignCenter)
        self.label_title.setMaximumHeight(self.TitlePointSize * 2)
        self.label_subtitle = QLabel("(*^▽^*) 单击按钮复制 PySide6 中生成代码到底部 (*^▽^*)")
        temp_font.setPointSize(self.SubTitlePointSize)
        temp_font.setBold(False)
        self.label_subtitle.setFont(temp_font)
        self.label_subtitle.setAlignment(Qt.AlignmentFlag.AlignCenter)
        self.label_subtitle.setMaximumHeight(self.SubTitlePointSize * 2)

        # 按钮
        self.layout_buttons = QGridLayout()  # 创建按钮的网格布局
        rowCount = -1
        columnCount = self.MaxColumn
        for index, each in enumerate(QStyle.StandardPixmap):
            if columnCount == self.MaxColumn:
                rowCount += 1  # 如果是曾经凑满一行，则+1行
                columnCount = 0  # 然后重置列计数

            button = QPushButton()
            button.setMinimumHeight(self.IconSize + 8)
            button.setIcon(self.style().standardIcon(each))
            button.setIconSize(QSize(self.IconSize, self.IconSize))
            button.setText(f"{str(index)} - {str(each).split('.')[1]}")
            button.clicked.connect(self.generate)  # 绑定触发生成方法
            self.layout_buttons.addWidget(button, rowCount, columnCount)  # 添加到网格

            columnCount += 1  # 列数加一

        # 输出项设置与输出框
        self.groupbox_Output = QGroupBox()  # 创建样式GroupBox
        self.layoutH_Settings = QHBoxLayout()  # 创建CheckBox的水平布局
        self.layoutH_LineEdits = QHBoxLayout()  # 创建LineEdit的水平布局
        self.layoutV_GroupBox = QVBoxLayout()  # 创建GroupBox中的垂直布局
        # groupbox_Output
        self.groupbox_Output.setTitle("输出")
        self.groupbox_Output.setLayout(self.layoutV_GroupBox)  # 设置GroupBox的布局为垂直
        # layoutH_Settings
        self.layoutV_GroupBox.addLayout(self.layoutH_Settings)
        self.checkBox_EnableClipBoard = QCheckBox("复制到剪切板（不包括序号）")
        self.checkBox_NeedPrefix = QCheckBox("保留前缀")
        self.checkBox_NeedPrefix.setChecked(True)
        self.pushbutton_PreviewIcon = QPushButton("查看图标")
        self.pushbutton_PreviewIcon.clicked.connect(self.preview_icon)
        for checkbox in [self.checkBox_EnableClipBoard, self.checkBox_NeedPrefix,
                         self.pushbutton_PreviewIcon]:  # 置入布局
            self.layoutH_Settings.addWidget(checkbox)
        # layoutV_Output其他部分
        self.label_Order = QLabel("")
        self.lineedit_Output = QLineEdit()
        self.lineedit_Output.setPlaceholderText("输出内容...先点一个按钮吧...")
        self.lineedit_Output.setReadOnly(True)
        self.layoutH_LineEdits.addWidget(self.lineedit_Output)
        self.layoutH_LineEdits.addWidget(self.label_Order)
        self.layoutV_GroupBox.addLayout(self.layoutH_LineEdits)

        # 主布局
        self.layout_Main = QVBoxLayout(self)
        self.layout_Main.addWidget(self.label_title)
        self.layout_Main.addWidget(self.label_subtitle)
        self.layout_Main.addLayout(self.layout_buttons)
        self.layout_Main.addWidget(self.groupbox_Output)
        if self.FixedWindowSize:
            self.setFixedSize(self.layout_Main.sizeHint())

    def generate(self):
        """
        生成代码内容
        """
        btn = self.sender()
        text = ""
        if self.checkBox_NeedPrefix.isChecked():
            text += "QStyle.StandardPixmap."
        splited = btn.text().split(" ")
        text += splited[2]
        if self.checkBox_EnableClipBoard.isChecked():
            pyperclip.copy(text)
        self.label_Order.setText(splited[0])
        self.lineedit_Output.setText(text)

    def preview_icon(self):
        """
        预览图标
        """
        order = self.label_Order.text()
        pixmap = list(enumerate(QStyle.StandardPixmap))[int(order)][1]  # 构造可迭代的StandardPixmap序列
        self.preview_window = QWidget()
        self.preview_window.setWindowTitle(f"图形预览")
        self.preview_window.setFixedSize(250, 250)
        layoutV_Preview = QVBoxLayout()
        label_Icon = QLabel()
        label_Icon.setAlignment(Qt.AlignmentFlag.AlignCenter)
        label_Icon.setPixmap(self.style().standardPixmap(pixmap).scaled(self.IconSize * 3, self.IconSize * 3))
        layoutV_Preview.addWidget(label_Icon)
        self.preview_window.setLayout(layoutV_Preview)
        self.preview_window.show()

    def closeEvent(self, event):
        """
        重写关闭事件
        使主窗口退出时结束程序
        """
        exit(0)


if __name__ == '__main__':
    app = QApplication([])
    window = Main_Window_Test()  # 构造登入窗口
    window.show()
    app.exec()
